define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        moment = require('moment'),
        helper = require('js/apps/report/common');

    var TacticsView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.TacticsReport();
            this.model.bind('change reset', this.renderWithData, this);
        },
        doSearch: function () {
            this.options.dateRange = helper.getDateRange();
            var rechargeLower = parseInt($('#rechargeLower').val()) || 0,
                rechargeUpper = parseInt($('#rechargeUpper').val()) || 0;
            if (rechargeLower || rechargeUpper) {
                this.options.rechargeRange = [rechargeLower];
                if (rechargeUpper) {
                    this.options.push(rechargeUpper);
                }
            }
            this.load();
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.model.list,
                total: this.model.total,
                options: this.options
            }));
            helper.loadDatePicker(this.options);
            var countOptions = chart.getHighChartPieOptions(),
                amountOptions = chart.getHighChartPieOptions(),
                countSeries = countOptions.series[0].data,
                amountSeries = amountOptions.series[0].data;
            countOptions.title.text = '人数占比';
            amountOptions.title.text = '金额占比';
            _.each(this.model.list, function (item) {
                countSeries.push({
                    name: item.id,
                    y: item.count
                });
                amountSeries.push({
                    name: item.id,
                    y: item.amount
                })
            });
            $('#countPie').highcharts(countOptions);
            $('#amountPie').highcharts(amountOptions);

            return this;
        },
        load: function () {
            var data = {
                date_range: this.options.dateRange.join(','),
            };
            if (this.options.rechargeRange) {
                data.recharge_range = this.options.rechargeRange.join(',')
            }
            this.model.fetch({
                reset: true,
                data: data,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            })
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/Tactics.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today],
                view = new TacticsView({
                    dateRange: dateRange
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});